package cn.wolfcode.ssm.web.controller;


import cn.wolfcode.ssm.domain.Appointment;
import cn.wolfcode.ssm.domain.Business;
import cn.wolfcode.ssm.domain.SystemDictionaryItem;
import cn.wolfcode.ssm.enums.AppointmentStatusEnum;
import cn.wolfcode.ssm.service.IAppointmentService;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.service.IBusinessService;
import cn.wolfcode.ssm.service.ISystemDictionaryItemService;
import cn.wolfcode.ssm.util.RequireAnnotation;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private IBusinessService businessService;


    @RequireAnnotation(name = "预约单页面",expression = "appointment:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        AppointmentStatusEnum[] enums = AppointmentStatusEnum.values();
        model.addAttribute("enums",enums);
        model.addAttribute("enums",enums);
        return "appointment/list";
    }

    @RequestMapping("/delete")
    @RequireAnnotation(name = "预约单删除",expression = "appointment:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            appointmentService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequireAnnotation(name = "预约单新增/编辑",expression = "appointment:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Appointment appointment){
        //后台加履行中的状态
        appointment.setStatus(AppointmentStatusEnum.PERFORM.getValue());
        if (appointment.getId() != null) {
            appointmentService.update(appointment);
        }else {
            appointmentService.save(appointment);
        }
        return new JsonResult();
    }

    //首页客户提交的预约（不需要权限）
    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save(Appointment appointment){
        //首页加待确认的状态
        appointment.setStatus(AppointmentStatusEnum.PEND.getValue());
        appointmentService.save(appointment);
        return new JsonResult();
    }



    @RequestMapping("/selectCategoryBySn")
    @ResponseBody
    public List<SystemDictionaryItem> selectCategoryBySn(String sn){
        if(StringUtils.hasText(sn)){
            return systemDictionaryItemService.selectCategoryBySn(sn);
        }
        return null;
    }
    @RequestMapping("/selectBusiness")
    @ResponseBody
    public List<Business> selectBusiness(){
        return businessService.listAll();
    }

    @RequireAnnotation(name = "预约单状态更新",expression = "appointment:updateStatus")
    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long id,Integer status){
        appointmentService.updateStatusById(id,status);
        return new JsonResult();
    }
}
